package com.ss.tmall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ss.tmall.pojo.Category;
import com.ss.tmall.pojo.Property;
import com.ss.tmall.service.CategoryService;
import com.ss.tmall.service.PropertyService;
import com.ss.tmall.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author: ShengShuai
 * @Description:
 * @Date: Created in 下午 21:20  2017/12/23 0023
 * @Modified By:
 */
@Controller
@RequestMapping("")
public class PropertyController {
@Autowired
    PropertyService propertyService;
@Autowired
    CategoryService categoryService;
@RequestMapping("admin_property_add")
    public String add(Model model, Property property){
    propertyService.add(property);
    return  "redirect:admin_property_list?cid="+property.getCid();
}
@RequestMapping("admin_property_delete")
public  String delete(int id){
      Property property=propertyService.get(id);
      propertyService.delete(id);
        return "redirect:admin_property_list?cid="+property.getCid();

}
@RequestMapping("admin_property_edit")
public  String edit(Model model,int id){
    Property property = propertyService.get(id);
    Category category= categoryService.get(property.getId());
    property.setCategory(category);
    model.addAttribute("p",property);
    return  "admin/editProperty";
}
@RequestMapping("admin_property_update")
public String update(Property property){
    propertyService.update(property);
    return "redirect:admin_property_list?cid="+property.getCid();

}
@RequestMapping("admin_property_list")
    public  String list(int cid, Model model, Page page){
    Category category= categoryService.get(cid);
    PageHelper.offsetPage(page.getStart(),page.getCount());
    List<Property> p = propertyService.list(cid);
    int total =  (int)new PageInfo<>(p).getTotal();
    page.setTotal(total);
    page.setParam("&cid="+category.getId());
    model.addAttribute("ps",p);
    model.addAttribute("c",category);
    model.addAttribute("page",page);
    return  "admin/listProperty";
}
}
